home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / rss / dosfind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  5.0 KB  |  168 lines

  1. /*
  2.     NON-MSDOS file name resulution emulation function.
  3.     
  4.     note: the _dos_findfirst/findnext emulation is controlled by the
  5.     define HAVE_GLOB. If your setup has a function glob(), set HAVE_GLOB in
  6.     main makefile and go for it. glob() must follow the syntaxis:
  7.     
  8.        #include <glob.h>
  9.        int glob(const char *pattern, int flags,
  10.         int errfunc(const char * epath, int eerrno),
  11.         glob_t *pglob);
  12.        void globfree(glob_t *pglob);
  13.        
  14.     This is the normal GNU C (Linux) glob() function.
  15.     
  16.     If you don't have glob(), don't set it. You will then get a poor man's
  17.     solution: a pipe to a command "echo <pattern>", from which filenames
  18.     are read.
  19. */
  20.  
  21. #ifndef MSDOS
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <ctype.h>
  27.  
  28. #ifdef HAVE_GLOB
  29. #   include <glob.h>
  30. #endif
  31.  
  32. #include "icrss.h"
  33.  
  34. #ifdef HAVE_GLOB
  35. static glob_t
  36.     gdata;                                      /* globbing struct */
  37. static int
  38.     nextglob;                                   /* next name in list */
  39. #else    
  40. static FILE
  41.     *fp = NULL;                                 /* echo program pipe */
  42. #endif
  43.  
  44. static char *filename (char *fname)             /* return pointer into */
  45. {                                               /* fname, just beyond */
  46.     register char                               /* directory name */
  47.         *cp;
  48.         
  49.     if ( 
  50.            (cp = strrchr (fname, DIRSEP))
  51.            &&
  52.            *(cp + 1)
  53.         )
  54.          return (cp + 1);
  55.          
  56.     return (fname);
  57. }
  58.  
  59. static int make_attrib (char *fname)            /* make DOS attribs */
  60. {
  61.     register int
  62.         ret = 0;                                /* returned attribute */
  63.     struct stat
  64.         statbuf;                                /* stat () buffer */
  65.         
  66.     if (stat (fname, &statbuf) == -1)           /* if not stat-able .. */
  67.         return (0xdead);                        /* dead flag return */
  68.  
  69.     if (statbuf.st_mode & S_IFDIR)              /* directory entry */
  70.         ret |= A_SUBDIR;
  71.         
  72.     if (!(statbuf.st_mode & S_IWRITE))          /* non-writable entry */
  73.         ret |= A_RDONLY;
  74.         
  75.     if (*fname == '.' &&                        /* .file */
  76.         strcmp (fname, ".") &&
  77.         strcmp (fname, "..")
  78.        )
  79.         ret |= A_HIDDEN;
  80.     
  81.     return (ret);                               /* return attrib */
  82. }
  83.  
  84. #ifdef HAVE_GLOB
  85. /* glob() error handler
  86.    returns 0: signal for glob to ignore the error 
  87. */
  88. static int globerr (const char *path, int errnr)
  89. {
  90.     return (0);
  91. }    
  92. #endif
  93.                                                 /* dos_findfirst emulator */
  94.                                                 /* ignores attribute! */
  95. unsigned _dos_findfirst(char * fspec, unsigned attrib,
  96.     struct _find_t * fileinfo)
  97. {
  98. #ifdef HAVE_GLOB
  99.     nextglob = 1;                               /* next globbed name */
  100.                                                 /* expand file spec */
  101.     glob (fspec, GLOB_NOCHECK, globerr, &gdata);
  102.     
  103.     if (! gdata.gl_pathc)                       /* no files: -1 return */
  104.         return (-1);
  105.     
  106.     strcpy (fileinfo->name, filename (gdata.gl_pathv[0]));
  107.                                                 /* synthetise attribute */
  108.     if ( (fileinfo->attrib = make_attrib (gdata.gl_pathv[0])) == 0xdead )
  109.         return (-1);
  110.     return (0);
  111. #else
  112.     char
  113.         buf[_MAX_PATH];                         /* filename buf */
  114.  
  115.     sprintf (buf, "echo %s", fspec);            /* open new pipe */
  116.     if (!(fp = popen(buf, "r")) ||              /* read first name */
  117.         !fscanf (fp, "%s", buf)
  118.        )
  119.     {
  120.         fclose (fp);
  121.         return -1;
  122.     }
  123.  
  124.     strcpy (fileinfo->name, filename (buf));    /* set name and attribute */
  125.     
  126.                                                 /* if non-existing.. */
  127.     if ((fileinfo->attrib = make_attrib (buf)) == 0xdead)
  128.         return (-1);
  129.         
  130.     return (0);                                 /* return success */
  131. #endif    
  132. }
  133.  
  134. unsigned _dos_findnext(struct _find_t * fileinfo)
  135. {
  136. #ifdef HAVE_GLOB
  137.     if (nextglob > gdata.gl_pathc)              /* done with list ? */
  138.     {
  139.         globfree (&gdata);                      /* yes.. free data */
  140.     return (-1);
  141.     }
  142.     
  143.     strcpy (fileinfo->name,                     /* make next name available */
  144.             filename (gdata.gl_pathv [nextglob]));
  145.                                             /* make attribute */
  146.     fileinfo->attrib = make_attrib (gdata.gl_pathv[nextglob]);
  147.     nextglob++;                                 /* set next name index */
  148.     return (0);
  149. #else
  150.     char
  151.         buf[_MAX_PATH];
  152.  
  153.     if (! fscanf (fp, "%s", buf) ||             /* if no more strings, */
  154.         feof (fp)                               /* or at EOF in pipe: */
  155.        )
  156.     {
  157.         fclose (fp);                            /* all done, */
  158.         return -1;
  159.     }
  160.  
  161.     strcpy (fileinfo->name, filename (buf));    /* set name, attrib */
  162.     fileinfo->attrib = make_attrib (buf);       /* file should exist now */
  163.     return (0);                                 /* return success */
  164. #endif    
  165. }
  166.  
  167. #endif
  168.